import lombok.Data;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.*;
import org.junit.jupiter.api.Test;

import java.time.Duration;

/**
 * @author ex_chenzy59
 * @since 2025/4/11
 */
public class 对象池 {
    @Test
    void t() {
        var factory=new PooledObjectFactory<ObjectAbc>() {
            @Override
            public void activateObject(PooledObject<ObjectAbc> pooledObject) throws Exception {
                /*借出对象时激活*/
            }

            @Override
            public void destroyObject(PooledObject<ObjectAbc> pooledObject) throws Exception {
                /*销毁对象时调用*/
            }

            @Override
            public PooledObject<ObjectAbc> makeObject() throws Exception {
                var item=new ObjectAbc();
                item.setCode("as");
                return new DefaultPooledObject<>(item);
            }

            @Override
            public void passivateObject(PooledObject<ObjectAbc> pooledObject) throws Exception {
                /*对象归还到池中时调用*/
            }

            @Override
            public boolean validateObject(PooledObject<ObjectAbc> pooledObject) {
                return false;
            }
        };
        var config=new GenericObjectPoolConfig<ObjectAbc>();
        config.setEvictionPolicy(new DefaultEvictionPolicy<>());
        config.setMaxIdle(1);
        config.setMaxTotal(2);
        config.setMaxWait(Duration.ofMinutes(1));
        config.setMinIdle(1);
//        config.setMinEvictableIdleTime();
        var abandonedConfig=new AbandonedConfig();
        var pool= new GenericObjectPool<>(factory, config,abandonedConfig);
//        pool.borrowObject();
    }
    @Data
    public static class ObjectAbc{
        private Integer id;
        private String code;
        private String name;
    }
}
